package com.zzuli.practice.service.impl;

import com.zzuli.practice.service.ILikeService;
import com.zzuli.practice.util.RedisKeyUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

@Service
public class LikeService implements ILikeService {

    @Autowired
    private RedisTemplate redisTemplate;

    // 点赞, 取消点赞
    public void like(String username, Integer postId){
        String likeKey = RedisKeyUtil.getPostLikeKey(postId);
        boolean isMember = redisTemplate.opsForSet().isMember(likeKey, username);
        if (isMember){
            redisTemplate.opsForSet().remove(likeKey, username);
        } else{
            redisTemplate.opsForSet().add(likeKey, username);
        }
    }

    // 查询用户的点赞状态
    public boolean likeStatus(String username, Integer postId){
        String likeKey = RedisKeyUtil.getPostLikeKey(postId);
        return redisTemplate.opsForSet().isMember(likeKey, username);
    }

    // 查询帖子点赞的数量
    public long findPostLikeCount(Integer postId){
        String likeKey = RedisKeyUtil.getPostLikeKey(postId);
        return redisTemplate.opsForSet().size(likeKey);
    }

}
